package com.study.get.jsl;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader.Feature;
import com.google.common.collect.Lists;
import com.study.get.jsl.BeileizhenDemo.TenTool;
import com.study.get.jsl.DataDTO.JslRowCell;
import com.study.get.jsl.JcInfoDemo.CninfoInter.ProBookInfo;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhuzi
 * 2025/3/20 下午4:02
 */
public class JcInfoDemo {
    @Test
    public void test1() {
        String body = FileUtil.readUtf8String("/Users/zhuzi/Downloads/kzz-24-06-14.json");
        List<JslRowCell> kzzDTOS = JSON.parseArray(JSON.parseObject(body).getString("rows"), JslRowCell.class, Feature.SupportSmartMatch);

        String stockName = kzzDTOS.stream().map(ia -> ia.getCell().getStockId()).collect(Collectors.joining(";"));
        String url = "http://www.cninfo.com.cn/new/information/getPrbookInfo";
        String block = TenTool.webClient.post().uri(url)
                .contentType(MediaType.APPLICATION_FORM_URLENCODED) // 设置Content-Type
                .body(BodyInserters.fromFormData("sectionTime", "2024-12-31") // 添加表单数据
                        .with("market", "szsh")//
                        .with("pagesize", "8000")
                        .with("pagenum", "1")
                )
                .header("Accept", "*/*").header("User-Agent", "tong hua shun/4.6.0 (Mac OS X ban ben12.7.6(ban hao21H1320))").retrieve().bodyToMono(String.class)//
                .timeout(Duration.ofSeconds(8)).retry(3).block();
        System.out.println(block);
        String string = JSON.parseObject(block).getString("prbookinfos");
        List<ProBookInfo> proBookInfos = JSON.parseArray(string, ProBookInfo.class, Feature.SupportSmartMatch, Feature.SupportAutoType);

        proBookInfos.stream().filter(i -> StringUtils.contains(i.getSecname(), "ST") || StringUtils.equals("", "")).forEach(System.out::println);
        String ding = proBookInfos.stream().map(ProBookInfo::ding).collect(Collectors.joining("\n"));
        //System.out.println(ding);
    }

    @Test
    public void test() {
        String body = FileUtil.readUtf8String("/Users/zhuzi/Downloads/kzz-24-06-14.json");
        List<JslRowCell> kzzDTOS = JSON.parseArray(JSON.parseObject(body).getString("rows"), JslRowCell.class, Feature.SupportSmartMatch);

        String url = "http://www.cninfo.com.cn/new/information/getPrbookInfo";
        String block = TenTool.webClient.post().uri(url)
                .contentType(MediaType.APPLICATION_FORM_URLENCODED) // 设置Content-Type
                .body(BodyInserters.fromFormData("sectionTime", "2024-12-31") // 添加表单数据
                        .with("market", "szsh")//
                        .with("pagesize", "8000")
                        .with("pagenum", "1")
                )
                .header("Accept", "*/*").header("User-Agent", "tong hua shun/4.6.0 (Mac OS X ban ben12.7.6(ban hao21H1320))").retrieve().bodyToMono(String.class)//
                .timeout(Duration.ofSeconds(8)).retry(3).block();
        System.out.println(block);
        String string = JSON.parseObject(block).getString("prbookinfos");
        List<ProBookInfo> proBookInfos = JSON.parseArray(string, ProBookInfo.class, Feature.SupportSmartMatch, Feature.SupportAutoType);
        for (ProBookInfo proBookInfo : proBookInfos) {
            //System.out.println(proBookInfo);
            if (StringUtils.contains(proBookInfo.getSecname(), "ST")) {
                System.out.println(proBookInfo.ding());
            }
        }
        String ding = proBookInfos.stream().map(ProBookInfo::ding).collect(Collectors.joining("\n"));
        //System.out.println(ding);
    }

    public interface CninfoInter {

        /**
         * http://www.cninfo.com.cn/new/commonUrl?url=data/yypl
         * 预约纰漏
         */
        @Data
        @Accessors(chain = true)
        class ProBookInfo {
            private String seccode;
            private String secname;
            /**
             * 首次预约
             */
            private String f002d_0102;
            /**
             * 初次变更
             */
            private String f003d_0102;
            /**
             * 二次变更
             */
            private String f004d_0102;
            /**
             * 三次变更
             */
            private String f005d_0102;
            /**
             * 实际披露
             */
            private String f006d_0102;


            public String equalString() {
                return seccode + f002d_0102 + "1" + f003d_0102 + "2" + f004d_0102 + "3" + f005d_0102 + "4" + f006d_0102;
            }

            public String ding() {
                ArrayList<String> strList = Lists.newArrayList(this.secname);
                if (StringUtils.isNotBlank(this.f006d_0102)) {
                    strList.add("实际披露 " + this.f006d_0102);
                    return StringUtils.join(strList, "\t");
                }
                if (StringUtils.isNotBlank(this.f005d_0102)) {
                    strList.add("三次变更 " + this.f005d_0102);
                    return StringUtils.join(strList, "\t");
                }
                if (StringUtils.isNotBlank(this.f004d_0102)) {
                    strList.add("二次变更 " + this.f004d_0102);
                    return StringUtils.join(strList, "\t");
                }
                if (StringUtils.isNotBlank(this.f003d_0102)) {
                    strList.add("初次变更 " + this.f003d_0102);
                    return StringUtils.join(strList, "\t");
                }
                if (StringUtils.isNotBlank(this.f002d_0102)) {
                    strList.add("首次预约 " + this.f002d_0102);
                    return StringUtils.join(strList, "\t");
                }
                return "";
            }

        }
    }

}
